﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TestApp.Flowchart.Model
{
    [JsonObject(MemberSerialization.OptIn)]
    class LayerNode2 : FlowNode
    {
        private int _recWidth = 50;
        [JsonProperty]
        public int RecWidth
        {
            get { return _recWidth; }
            set
            {
                _recWidth = value;
                OnPropertyChanged("RecWidth");
            }
        }
        private int _recHeight = 50;
        [JsonProperty]
        public int RecHeight
        {
            get { return _recHeight; }
            set
            {
                _recHeight = value;
                OnPropertyChanged("RecHeight");
            }
        }

        private int _margin = 8;
        [JsonProperty]
        public int Margin
        {
            get { return _margin; }
            set
            {
                _margin = value;
                OnPropertyChanged("Margin");
            }
        }

        private string _color1 = "255, 157, 157, 157";
        [JsonProperty]
        public string Color1
        {
            get { return _color1; }
            set
            {
                _color1 = value;
                OnPropertyChanged("Color1");
            }
        }

        private string _color2 = "255, 102, 102, 102";
        [JsonProperty]
        public string Color2
        {
            get { return _color2; }
            set
            {
                _color2 = value;
                OnPropertyChanged("Color2");
            }
        }

        private int layerNO = 6;
        [JsonProperty]
        public int LayerNO
        {
            get { return layerNO; }
            set
            {
                layerNO = value;
                OnPropertyChanged("LayerNO");
            }
        }

        public LayerNode2() { }

        public override FrameworkElement CreateContent()
        {
            var textBlock = base.CreateContent() as TextBlock;

            var canvas = new Canvas();

            bool isOdd = true;
            for (int i = 0; i < layerNO; i++)
            {
                Rectangle rect = new Rectangle()
                {
                    Width = _recWidth,
                    Height = _recHeight,
                };

                SolidColorBrush mySolidColorBrush = new SolidColorBrush();
                if (isOdd)
                {
                    mySolidColorBrush.Color = UIUtils.convertRGB(this._color1);                  
                } else
                {
                    mySolidColorBrush.Color = UIUtils.convertRGB(this._color2);
                }

                rect.Fill = mySolidColorBrush;

                isOdd = !isOdd;

                Canvas.SetLeft(rect, i * _margin);
                Canvas.SetTop(rect, i * _margin);

                canvas.Children.Add(rect);
            }

            Canvas.SetLeft(textBlock, _recWidth/2);
            Canvas.SetTop(textBlock, (layerNO - 1) * _margin + _recHeight + 10);

            canvas.Children.Add(textBlock);

            return canvas;
        }

        public FlowNode Json2Obj(string value)
        {
            return JSONUtils.Json2Obj<LayerNode2>(value);
        }
        
    }
}
